package org.zjt.platform.spring.boot.example.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Objects;

/**
 * Created by zhang on 2017/2/28.
 */
@ApiModel("返回的json对象")
@Data
public class JsonResult<T> implements Serializable{

    public static final String SUCCESS_MSG="ok";


    private T data;

    @ApiModelProperty("请求是否成功 true:成功  false：失败")
    private Boolean success;

    @ApiModelProperty("返回的信息")
    private String msg;

    @ApiModelProperty("是否存在data数据 true：是  false：否")
    private Boolean has_data;

    @ApiModelProperty("错误码")
    private Integer error_code;

    private JsonResult(){}



    public static<T> JsonResult<T> OK(T data) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setData(data);
        jsonResult.setMsg(SUCCESS_MSG);
        jsonResult.setSuccess(true);
        jsonResult.setError_code(HttpStatus.OK.value());
        jsonResult.setHas_data(Objects.nonNull(data));
        return jsonResult;
    }

    public static<T> JsonResult<T> OK() {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setMsg(SUCCESS_MSG);
        jsonResult.setSuccess(true);
        jsonResult.setError_code(HttpStatus.OK.value());
        jsonResult.setHas_data(Boolean.FALSE);
        return jsonResult;
    }


    public static<T> JsonResult<T> ERROR(String errorMsg) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setMsg(errorMsg);
        jsonResult.setSuccess(Boolean.FALSE);
        jsonResult.setError_code(HttpStatus.INTERNAL_SERVER_ERROR.value());
        jsonResult.setHas_data(Boolean.FALSE);
        return jsonResult;
    }
}
